package io.renren.api.menu;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import io.renren.core.annotation.LoginUser;
import io.renren.entity.Dictionary;
import io.renren.entity.UserDefaultInfo;
import io.renren.entity.WxUser;
import io.renren.entity.dto.GradeSubjectDto;
import io.renren.entity.dto.MenuBookVideoDto;
import io.renren.service.BookVideoService;
import io.renren.service.DictionaryService;
import io.renren.service.GradeSubjectService;
import io.renren.service.UserDefaultInfoService;
import io.renren.utils.ConfigConstant;
import io.renren.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个性化菜单视频controller
 *
 * @Author: hongyuliu
 * @Date: 2020-05-08
 * @Version 1.0
 */
@RestController
@RequestMapping("/person/menu")
public class PersonalizedMenuBookVideoController {

    @Autowired
    private BookVideoService bookVideoService;

    @Autowired
    private GradeSubjectService gradeSubjectService;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private UserDefaultInfoService userDefaultInfoService;

    /**
     * 根据科目获取列表
     *
     * @param gradeId
     * @return
     */
    @GetMapping("/subject/list")
    public R getSubjectListByGrade(@RequestParam Long gradeId) {
        List<GradeSubjectDto> gradeSubjectDtos = gradeSubjectService.querySubjectList(gradeId);
        return R.ok().put("subjectList", gradeSubjectDtos);
    }

    @GetMapping("/default/version/{gradeId}")
    public R getDefaultVersion(@PathVariable Long gradeId, @LoginUser WxUser wxUser) {
        Map query = new HashMap();
        query.put("gradeId", gradeId);
        query.put("userId", wxUser.getId());
        query.put("seasonId", ConfigConstant.DEFAULT_SEASON_ID);
        List<UserDefaultInfo> userDefaultInfoList = userDefaultInfoService.queryList(query);
        Map result = new HashMap();
        if (CollectionUtil.isNotEmpty(userDefaultInfoList)) {
            for (UserDefaultInfo userDefaultInfo : userDefaultInfoList) {
                result.put(userDefaultInfo.getSubjectId(),userDefaultInfo.getVersionId());
            }
        }
        return R.ok().put("result",result);
    }

    @GetMapping("/version/list")
    public R getVersionList() {
        Map map = new HashMap();
        map.put("type", "T03");
        map.put("status", 1);
        map.put("deleted", 0);

        List<Dictionary> list = dictionaryService.queryList(map);
        CollectionUtil.sort(list, Comparator.comparing(Dictionary::getOrderField));
        return R.ok().put("versionList", list);
    }

    /**
     * 根据年级科目教版获取视频列表
     *
     * @return
     */
    @RequestMapping("/video/list")
    public R getVideoList(@RequestParam Long gradeId) {
        Map<Long, Map<Long, List<MenuBookVideoDto>>> subjectVersionVideoMap =
                bookVideoService.getSubjectVersionVideoMap(gradeId);

        return R.ok().put("subjectVersionVideoMap", subjectVersionVideoMap);
    }

}
